package org.aes;

import org.aes.util.CAESEncrypt;
import org.aes.util.JavaAESEncrypt;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

/**
 * 加密解密Demo<br/>
 * 一般Android客户端使用JNI加密解密，服务器使用java加密解密
 * @author 修行者
 *
 */
public class DemoActivity extends Activity {
	
	TextView result;
	
	String log = "";
	
	String data = "{[{\"name\":\"jim\", \"age\":16},{\"name\":\"jerry\", \"age\":18},{\"name\":\"kate\", \"age\":21}," +
			"{\"name\":\"jack\", \"age\":20},{\"name\":\"tim\", \"age\":19},{\"name\":\"green\", \"age\":22}]}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        
        result = (TextView) findViewById(R.id.result);
        
        JniToJava();
        JavaToJni();
    }
    
    /**
     * Jni加密，java解密
     */
    private void JniToJava(){
    	log += "JniToJava : \n原文 : " + data + "\n";
    	log += "压缩前 : " + data.getBytes().length + "\n";
    	// 压缩数据，也可以不压缩，直接加密
    	byte[] zipData = CAESEncrypt.compress(data.getBytes());
    	log += "压缩后 : " + zipData.length + "\n";
    	// 加密数据
    	byte[] resultData = CAESEncrypt.AESencrypt(zipData);
    	log += "加密后 : " + resultData.length + "\n";
    	
    	try {
    		// 解密数据
    		byte[] zDate = JavaAESEncrypt.DecodeAESEncrypt(resultData);
    		// 解压数据
    		String resultDate = JavaAESEncrypt.uncompress(zDate);
    		log += "解密后 : " + resultDate + "\n\n";
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	result.setText(log);
    }
    
    /**
     * Java加密，Jni解密
     */
    private void JavaToJni(){
    	log += "JniToJava : \n原文 : " + data + "\n";
    	log += "压缩前 : " + data.getBytes().length + "\n";
    	try {
	    	// 压缩数据，也可以不压缩，直接加密
	    	byte[] zipData = JavaAESEncrypt.compress(data);
	    	log += "压缩后 : " + zipData.length + "\n";
	    	// 加密数据
	    	byte[] resultData = JavaAESEncrypt.AESencrypt(zipData);
	    	log += "加密后 : " + resultData.length + "\n";
	    	
			// 解密数据
			byte[] zDate = CAESEncrypt.DecodeAESEncrypt(resultData);
			// 解压数据
			String resultDate = new String(CAESEncrypt.uncompress(zDate));
			log += "解密后 : " + resultDate + "\n\n";
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	result.setText(log);
    }
}
